package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JTextField;
import java.awt.Font;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JPanel;
import javax.swing.JPasswordField;

import GUI.SimpleTable;
import controllers.SGIsettings;
import controllers.ServerUIP;
//import controllers.WelcomeUIP;

import javax.swing.ImageIcon;

public class ServerUI extends JFrame  implements ActionListener, KeyListener
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ServerUIP ServerUIP;
	private JPanel contentPane;
	
	private JTextField txtPort;
	private JTextField txtDBIP;
	private JTextField txtDBPort;
	private JTextField txtDBUsername;
	private JTextField txtDBSchemeName;
	private JPasswordField txtDBPassword;
	
	private JButton btnStart;
	private JButton btnStop;
	private JButton btnExit;
	
	public SimpleTable activityTable ;
	public final static ImageIcon IMG_SERVER_BACKGROUND = new ImageIcon("images/Server Background.jpg");
	private JLabel label;
	
	/**
	 * Launch the application.
	 */
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ServerUI frame = new ServerUI();
					frame.setVisible(true);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ServerUI() 
	{
		initialize();
	}

	/**
	 * Initialize the contents of the contentPane.
	 */
	private void initialize() 
	{
		// Create the UIP layer.
		ServerUIP = new ServerUIP(this);

		this.setTitle("SGI SYSTEM LOGIN SERVER");
	
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		setBounds(100, 100, 492, 492);
		
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new WindowAdapter() 
			{
				public void windowClosing(WindowEvent e) 
				{
					ServerUIP.exit();
				}
			});
		contentPane = new JPanel();
		setContentPane(contentPane);
		contentPane.setLayout(null);
		setResizable(false);
		setLocation(800, 300);
		
		//=========================================================================
		//			New table for displaying the activity log.
		//=========================================================================
		
		Object Arr[][]=null;
		Object ColNames[] = { "Date", "Message" };
	    activityTable = new SimpleTable(Arr, ColNames, 10, 302, 466, 151);
	    activityTable.setColWidth(1, 300);
	    contentPane.add(activityTable.tblScrollPane,BorderLayout.CENTER);
	    
		JLabel lblActivityLog = new JLabel("Activity log");
		lblActivityLog.setBounds(10, 280, 89, 14);
		contentPane.add(lblActivityLog);
		
		btnStart = new JButton("Start");
		
		
		btnStart.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				startClicked();
			}
		});
		
		btnStart.setBounds(23, 60, 89, 29);
		contentPane.add(btnStart);
		
		btnStop = new JButton("Stop");
		
		btnStop.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				stopClicked();
			}
		});
		
		
		btnStop.setBounds(23, 102, 89, 29);
		btnStop.setEnabled(false);
		contentPane.add(btnStop);
		
		JLabel lblServiceControl = new JLabel("Service Control");
		lblServiceControl.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblServiceControl.setBounds(23, 35, 106, 14);
		contentPane.add(lblServiceControl);
		
		JLabel lblServiceSettings = new JLabel("Service Settings");
		lblServiceSettings.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblServiceSettings.setBounds(23, 206, 130, 20);
		contentPane.add(lblServiceSettings);
		
		txtPort = new JTextField("5555");
		txtPort.setBounds(139, 237, 96, 20);
		contentPane.add(txtPort);
		txtPort.setColumns(10);
		
		JLabel lblPort = new JLabel("Port:");
		lblPort.setBounds(33, 240, 120, 14);
		contentPane.add(lblPort);
		
		JLabel lblDbSettings = new JLabel("DB Settings");
		lblDbSettings.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblDbSettings.setBounds(234, 35, 130, 23);
		contentPane.add(lblDbSettings);
		
		JLabel lblIp = new JLabel("IP Address:");
		lblIp.setBounds(244, 70, 120, 14);
		contentPane.add(lblIp);
		
		txtDBIP = new JTextField("localhost");
		txtDBIP.setColumns(10);
		txtDBIP.setBounds(350, 67, 96, 20);
		contentPane.add(txtDBIP);
		
		JLabel lblPort_1 = new JLabel("Port:");
		lblPort_1.setBounds(244, 98, 120, 14);
		contentPane.add(lblPort_1);
		
		txtDBPort = new JTextField("3306");
		txtDBPort.setColumns(10);
		txtDBPort.setBounds(350, 95, 96, 20);
		contentPane.add(txtDBPort);
		
		JLabel lblUsername = new JLabel("Username:");
		lblUsername.setBounds(244, 155, 120, 14);
		contentPane.add(lblUsername);
		
		txtDBUsername = new JTextField("root");
		txtDBUsername.setColumns(10);
		txtDBUsername.setBounds(350, 152, 96, 20);
		contentPane.add(txtDBUsername);
		
		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(244, 183, 120, 14);
		contentPane.add(lblPassword);
		
		txtDBPassword = new JPasswordField("root");
		txtDBPassword.setBounds(350, 180, 96, 20);
		contentPane.add(txtDBPassword);
		
		btnExit = new JButton("Exit");
		btnExit.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				exitClicked();
			}
		});
		
		btnExit.setBounds(23, 144, 89, 29);
		contentPane.add(btnExit);
		
		JLabel lblSchemeName = new JLabel("Scheme:");
		lblSchemeName.setBounds(244, 123, 60, 14);
		
		contentPane.add(lblSchemeName);
		
		txtDBSchemeName = new JTextField("obl");
		txtDBSchemeName.setBounds(350, 121, 96, 20);
		contentPane.add(txtDBSchemeName);
		
		label = new JLabel("");
		label.setIcon(SGIsettings.IMG_SERVER_BACKGROUND);
		label.setBounds(0, 0, 486, 464);
		contentPane.add(label);
		
	}
	
	// Getters & Setters
	
	public int getPort()
	{
		return Integer.parseInt(txtPort.getText());
 
	}

	public String getDBIP()
	{
		return txtDBIP.getText();
	}
	
	public String getDBUsername()
	{
		return txtDBUsername.getText();
	}
	
	
	public char[] getDBPassword()
	{
		return txtDBPassword.getPassword();
	}
	
	public int getDBPort()
	{
		return Integer.parseInt(txtDBPort.getText());
	}
	public String getDBSchemeName()
	{
		return txtDBSchemeName.getText();
	}
	
	// .....................................................................
	// Functions calling UIP Layer to perform logic behind this commands
	// .....................................................................
	
	private void startClicked()
	{
		ServerUIP.start();
	}
	private void exitClicked()
	{
		ServerUIP.exit();
	}
	private void stopClicked()
	{
		ServerUIP.stop();
	}	
	
	
	// This function will display messages about events and errors at the Activity Log list.
	public void msgConsole(String msg)
	{
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		Date date = new Date();
		String datetime = dateFormat.format(ServerUIP.clk.getTime());

		activityTable.model.addRow(new Object[] {datetime,msg});
		int lastIndex = activityTable.table.getRowCount();
		activityTable.table.changeSelection(lastIndex-1, 0, false, false);
		activityTable.table.scrollRectToVisible(activityTable.table.getCellRect(lastIndex-1, 0, true));
		activityTable.table.repaint();
	}
	
	
	
	public boolean isComponentEnabled(JComponent c)
	{
		return c.isEnabled();
	}
	
	public boolean isStartEnabled()
	{
		return btnStart.isEnabled();
	}
	public void setStartEnable(boolean s)
	{
		btnStart.setEnabled(s);
	}
	public void setStopEnable(boolean s)
	{
		btnStop.setEnabled(s);
	}
	public void setExitEnable(boolean s)
	{
		btnExit.setEnabled(s);
	}
	public void setSettings(boolean s)
	{
		txtDBIP.setEnabled(s);
		txtDBPort.setEnabled(s);
		txtDBPassword.setEnabled(s);
		txtDBSchemeName.setEnabled(s);
		txtDBUsername.setEnabled(s);
		txtPort.setEnabled(s);			
	}
	
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if (e.getSource() == btnStart)
		{
	//			ServerUIP.HandleUIEvent(ServerUIP.START);
		} // end of Start button 
		if (e.getSource() == btnStop)
		{
		//	ServerUIP.HandleUIEvent(ServerUIP.STOP);
		} // end of Stop button 
		if (e.getSource() == btnExit)
		{
			//ServerUIP.HandleUIEvent(ServerUIP.EXIT);
		} // end of Exit button 
	}

	@Override
	public void keyPressed(KeyEvent e) 
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyReleased(KeyEvent e) 
	{
		System.out.println("UI@Server: keyRelease.");
		if (e.getSource() == txtPort) 
		{
			//ServerUIP.HandleUIEvent(ServerUIP.KeyRelease_Port);
		}
		if (e.getSource() == txtDBIP) 
		{
			//ServerUIP.HandleUIEvent(ServerUIP.KeyRelease_DB_IP);
		}
		if (e.getSource() == txtDBPort) 
		{
			//ServerUIP.HandleUIEvent(ServerUIP.KeyRelease_DB_Port);
		}
		if (e.getSource() == txtDBSchemeName) 
		{
			//ServerUIP.HandleUIEvent(ServerUIP.KeyRelease_DB_Name);
		}
		if (e.getSource() == txtDBUsername) 
		{
			//ServerUIP.HandleUIEvent(ServerUIP.KeyRelease_DB_User);
		}
		if (e.getSource() == txtDBPassword) 
		{
			//ServerUIP.HandleUIEvent(ServerUIP.KeyRelease_DB_Pass);
		}		
	}

	@Override
	public void keyTyped(KeyEvent e) 
	{
		// TODO Auto-generated method stub
		
	}
}
